package com.itheima.java.ai.langchin4j.server.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.java.ai.langchin4j.entity.User;
import com.itheima.java.ai.langchin4j.mapper.UserMapper;
import com.itheima.java.ai.langchin4j.server.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;

@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {
    
    @Autowired
    private UserMapper userMapper;
    
    private final BCryptPasswordEncoder passwordEncoder = new BCryptPasswordEncoder();
    
    @Override
    public boolean register(User user) {
        // 检查用户名、邮箱、手机号、身份证号是否已存在
        if (existsByUsername(user.getUsername())) {
            throw new RuntimeException("用户名已存在");
        }
        if (existsByEmail(user.getEmail())) {
            throw new RuntimeException("邮箱已存在");
        }
        if (existsByPhone(user.getPhone())) {
            throw new RuntimeException("手机号已存在");
        }
        if (existsByIdCard(user.getIdCard())) {
            throw new RuntimeException("身份证号已存在");
        }
        
        // 加密密码
        user.setPassword(passwordEncoder.encode(user.getPassword()));
        
        // 设置默认角色
        if (user.getRole() == null) {
            user.setRole(User.Role.PATIENT);
        }
        
        // 设置创建时间
        user.setCreatedAt(LocalDateTime.now());
        user.setUpdatedAt(LocalDateTime.now());
        
        return save(user);
    }
    
    @Override
    public User login(String username, String password) {
        User user = findByUsernameOrEmail(username);
        if (user == null) {
            throw new RuntimeException("用户不存在");
        }
        
        if (!passwordEncoder.matches(password, user.getPassword())) {
            throw new RuntimeException("密码错误");
        }
        
        return user;
    }
    
    @Override
    public User findByUsernameOrEmail(String username) {
        return userMapper.findByUsernameOrEmail(username);
    }
    
    @Override
    public boolean existsByUsername(String username) {
        LambdaQueryWrapper<User> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(User::getUsername, username);
        return count(queryWrapper) > 0;
    }
    
    @Override
    public boolean existsByEmail(String email) {
        return userMapper.findByEmail(email) != null;
    }
    
    @Override
    public boolean existsByPhone(String phone) {
        return userMapper.findByPhone(phone) != null;
    }
    
    @Override
    public boolean existsByIdCard(String idCard) {
        return userMapper.findByIdCard(idCard) != null;
    }
    
    @Override
    public boolean updateUserInfo(User user) {
        user.setUpdatedAt(LocalDateTime.now());
        return updateById(user);
    }
}